package com.dundun.SSO.core.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import java.io.InputStream;
import java.util.Properties;

public class InitInfoListener extends ContextLoaderListener {

	private static final Logger logger = LoggerFactory.getLogger(InitInfoListener.class);

	private ApplicationContext ctx;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);

        ServletContext sc = event.getServletContext();
		printVersion(sc);

        this.ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
        if (null != ctx) {

        }
    }

	private void printVersion(ServletContext application) {
		try {
			InputStream in = application.getResourceAsStream("/META-INF/MANIFEST.MF");
			if (in != null) {
				Properties manifest = new Properties();
				manifest.load(in);

				String revision = manifest.getProperty("Build-Revision", "Unknown");
				String time = manifest.getProperty("Build-Time", "Unknown");
				logger.info("当前系统版本：Build-Revision(" + revision + ") Build-Time(" + time + ")");
			}
		} catch (Exception e) {
			logger.warn("加载系统版本信息错误：", e);
		}
	}
	
}
